<?php
include dirname(dirname(dirname(__FILE__))).'/include/db_connect.php';

function checkInit(){
	$conn=get_db_conn();
	$sql="select count(*) as cnt from place_apply_table";
	$ret = mysql_query($sql,$conn);
	mysql_close($conn);
	$row=mysql_fetch_array($ret);
	if ($row["cnt"]==0) {
		return 1;
	}else return 0; 

}
function Init(){
	$conn=get_db_conn();
	$i=0;
	while ($i<7){
	$t=date('Y-m-d',strtotime("+".$i."day"));
	$sql='insert into place_apply_table(date) values('.'"'.$t.'"'.')';
	 if(mysql_query($sql,$conn)){
	 	$i++;
	 }
	}
	mysql_close($conn);
	
}
function deleteCurrentDateInfo(){
	$conn=get_db_conn();
	$sql='select id from place_apply_table limit 0,1';
	$ret=mysql_query($sql,$conn);
	if (!$ret) {
		echo mysql_error();
		mysql_close($conn);
		return false;
	}
	$row=mysql_fetch_array($ret);
	$place_apply_table_id=$row['id'];
	$sql='select count(*)  as cnt from place_apply where place_apply_table_id="'.$place_apply_table_id.'"';
	$ret=mysql_query($sql,$conn);
	if (!$ret) {
		echo mysql_error();
		mysql_close($conn);
		return false;
	}
	$row=mysql_fetch_array($ret);
	if ($row["cnt"]>0) {
	
	$sql='select id from place_apply where place_apply_table_id="'.$place_apply_table_id.'"';
	$ret=mysql_query($sql,$conn);
	if (!$ret) {
		echo mysql_error();
		mysql_close($conn);
		return false;
	}
	while ($row=mysql_fetch_array($ret)){
		$sql='select * from accessory where apply_id='.$row['id'];
		$ret1=mysql_query($sql,$conn);
		if (!$ret1) {
			echo mysql_error();
			mysql_close($conn);
			return false;
		}
		while ($row1=mysql_fetch_array($ret1)){
			if (unlink(dirname(dirname(dirname(__FILE__)))."/front-show/accessory/".$row1['dir']."/".$row1['name'])){
				$sql='delete from accessory where id='.$row1["id"];
				$ret2=mysql_query($sql,$conn);
				if (!$ret2) {
					echo mysql_error();
					mysql_close($conn);
					return false;
					
				}
			}
			
		}
		$sql='delete from place_apply where id='.$row["id"];
		$ret2=mysql_query($sql,$conn);
		if (!$ret2) {
			echo mysql_error();
			mysql_close($conn);
			return false;
				
		}
		
		
	}

}
$sql='delete from place_apply_table where id='.$place_apply_table_id;
$ret2=mysql_query($sql,$conn);
if (!$ret2) {
	echo mysql_error();
	mysql_close($conn);
	return false;

}
	mysql_close($conn);
	return true;
	

	
}
function addNewDateInfo(){
	$conn=get_db_conn();
	$t=date('Y-m-d',strtotime("+6 day"));
	$sql='insert into place_apply_table(date) values('.'"'.$t.'"'.')';
	$ret=mysql_query($sql,$conn);
	if (!$ret) {
		echo mysql_error();
		mysql_close($conn);
		return false;
	}
	mysql_close($conn);
	return true;
	
	
}
function updateDateInfo(){
	$conn=get_db_conn();
	$sql='select * from place_apply_table limit 1,1';
	$ret=mysql_query($sql,$conn);
	if (!$ret) {
		echo mysql_error();
		mysql_close($conn);
		return false;
	}
	$row=mysql_fetch_array($ret);
	$sql='select count(*) as cnt from place_apply where place_apply_table_id='.$row['id'].' and status=0';
	$ret=mysql_query($sql,$conn);
	if (!$ret) {
		echo mysql_error();
		mysql_close($conn);
		return false;
	}
	$row1=mysql_fetch_array($ret);
	
	if ($row1["cnt"]>0) {
	
	$sql='update place_apply set status=2 where place_apply_table_id='.$row["id"].' and status=0';
	$ret=mysql_query($sql,$conn);
	if (!$ret) {
		echo mysql_error();
		mysql_close($conn);
		return false;
	}
	}
	mysql_close($conn);
	return true;
	
	
	
}



/**
 * 
 * @param unknown $id
 * @return resource|boolean
 */
function getPlaceApplyTableById($id){
	
	$conn=get_db_conn();
	$sql="select * from place_apply_table where id=".$id.";";
	$ret = mysql_query($sql,$conn);
	if ($ret) {
		mysql_close($conn);
		return $ret;
	}else {
		echo "error: ".mysql_error();
		mysql_close($conn);
		return false;
	}
	
}
/**
 * 
 * @param unknown $params
 * @param unknown $id
 * @return boolean
 */
function updatePlaceApplyTable($params,$id){
	$sql = "update place_apply_table set ";
	$tmp = "";
	foreach ($params as $key => $item) {
		$tmp.=",".$key." = ".$item;
	}
	$sql = $sql.substr($tmp, 1).' where id = '.$id.';';
	$conn = get_db_conn();
	if(mysql_query($sql,$conn)) return true;
	else  {
		echo mysql_error();
		return false;}
}
/**
 * 
 * @param unknown $params
 * @param unknown $id
 * @return boolean
 */
function updateApply($params,$id){
	$sql = "update place_apply set ";
	$tmp = "";
	foreach ($params as $key => $item) {
		$tmp.=",".$key." = ".$item;
	}
	$sql = $sql.substr($tmp, 1).' where id = '.$id.';';
	$conn = get_db_conn();
	if(mysql_query($sql,$conn)) return true;
	else  return false;
}
/**
 * 
 * @param unknown $id
 * @param unknown $period
 */
function applyListByPatId($id, $period){
	$conn=get_db_conn();
	$sql='select  * from place_apply 
	where place_apply_table_id="'.$id.'"
	and period='.$period;
	$ret = mysql_query($sql,$conn);
	if ($ret) {
	
		mysql_close($conn);
		return $ret;
	
	
	}else {
		echo "error: ".mysql_error();
		mysql_close($conn);
		return false;
	}
	
	
}
/**
 * 
 * @param unknown $date
 * @param unknown $period
 * @return Ambigous <>|boolean
 */
function applyList($date,$period){

	

	$conn=get_db_conn();
	/*$sql='select pat.id as pat_id ,pat.table_content,pat.date,pa.*
	 from place_apply_table pat
	left join place_apply pa
	on pa.place_apply_table_id=pat.id
	where pat.date="2014-09-08"
	and pa.period=2';*/
	$sql='select  *,pat.id as pat_id,pat.table_content,pat.date,
	pa.id as pa_id,pa.s_user_id,pa.status,pa.place_apply_table_id,
	pa.period,pa.create_time,s_u.* from place_apply_table pat
	left join place_apply pa
	on pa.place_apply_table_id=pat.id
	left join s_user s_u on pa.s_user_id=s_u.id
	where pat.date="'.$date.'"
	and pa.period='.$period;
	$ret = mysql_query($sql,$conn);
	if ($ret) {
		

			mysql_close($conn);
			return $ret;

	
	}else {
		echo "error: ".mysql_error();
		mysql_close($conn);
		return false;
	}




}

/**
 * 
 * @param unknown $date
 * @param unknown $period
 * @return multitype:string Ambigous <> |boolean
 * 0表示有占用，1表示待审核
 */
function applyCheck($date,$period){
	
	$conn=get_db_conn();
	/*$sql='select pat.id as pat_id ,pat.table_content,pat.date,pa.*
	from place_apply_table pat
	left join place_apply pa
	on pa.place_apply_table_id=pat.id
	where pat.date="2014-09-08"
	and pa.period=2';*/
	$sql='select count(*) as cnt
	from place_apply_table pat
	left join place_apply pa
	on pa.place_apply_table_id=pat.id
	where pat.date="'.$date.'"
	and pa.period='.$period.' and status=0';
	$ret = mysql_query($sql,$conn);
	if ($ret) {
		while ($row=mysql_fetch_array($ret)){
		
				mysql_close($conn);
				return $row["cnt"];
				
		}
	}else {
		echo "error: ".mysql_error();
		mysql_close($conn);
		return false;
	}
	
	

	
}
/**
 * 
 * @return resource|boolean
 */
function getPlaceApplyTable(){
	$conn=get_db_conn();
	$sql="select * from place_apply_table;";
	$ret = mysql_query($sql,$conn);
	if ($ret) {
		mysql_close($conn);
		return $ret;
	}else {
		echo "error: ".mysql_error();
		mysql_close($conn);
		return false;
	}

}
/**
 * 
 * @param unknown $id
 * @return resource|boolean
 */
function  getSUserById($id){
	$conn = get_db_conn();
	$sql = "select * from s_user where id='".$id."' limit 0,1;";
	$ret = mysql_query($sql,$conn);
	if ($ret) {
		mysql_close($conn);
		return $ret;
	}else {
		echo "error: ".mysql_error();
		mysql_close($conn);
		return false;
	}


}
?>